package com.tucc.hello.leetcode.no1_400.no201_400.no215;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 8, 2, 4, 9};
        int k = 2;
        System.out.println(solution.findKthLargest(nums, k));
    }

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for(int i = 0;i < nums.length;i ++){
            if(queue.size() < k){
                queue.offer(nums[i]);
            }
            else{
                int max = queue.peek();
                if(nums[i] > max){
                    queue.poll();
                    queue.offer(nums[i]);
                }
            }
        }
        return queue.peek();
    }
}
